/*
*
* 节流：
* 主要思想：如果持续触发事件，则每隔一段事件，触发一次事件
* */
// 顾头不顾尾
function throttle(fun,wait){
    let result;
    let old = 0;
    let now = 0;
    return function(){
        let content = this;
        let args = arguments;
        now = new Date().valueOf();
        if (now - old >= wait){
            result = fun.apply(content,args)
            old = now;
        }
        return result;
    }
}

//顾尾不顾头
function timeoutThrottle(fun,wait){
    let result;
    let timeout;
    return function(){
        let content = this;
        let args = arguments;
        if (!timeout){
            timeout = setTimeout(()=>{
                result = fun.apply(content,args);
                timeout = null;
            },wait);
        }
        return result;
    }
}

//顾头顾尾
function throttleReform(fun,wait){
    let result;
    let timeout;
    let now;
    let old = 0;
    return function(){
        let content = this;
        let args = arguments;
        now = new Date().valueOf();
        if (now - old > wait){
            if (timeout){
                clearTimeout(timeout);
                timeout = null;
            }
            result = fun.apply(content,args);
            old = now;
        }
        if (!timeout){
            timeout = setTimeout(()=>{
                old = new Date().valueOf();
                result = fun.apply(content,args);
                timeout = null;
            },wait)
        }
        return result;
    }
}

let dec = document.querySelector('#dec');
let number = document.querySelector('#number');
let add = document.querySelector('#add');
let container = document.querySelector('#container');

container.addEventListener('mousemove',throttleReform(function(){
    number.innerHTML = Number(number.innerHTML) + 1;
},1000));

